<template>
  <div class="main" style="margin: 10px">
    <el-card>
      <div>
        <!--工具栏-->
        <div class="head-container">
          <div>
            <!-- 搜索 -->
            <span>
              <el-input v-model="searchKey" clearable placeholder="请输入用户名" style="width: 200px" class="filter-item" @keyup.enter.native="toQuery" />
              <span class="filter-item">
                <el-button type="success" icon="el-icon-search" @click="searchFilter">搜索</el-button>
                <!-- <el-button type="warning" icon="el-icon-refresh-left">重置</el-button> -->
                <el-button type="primary" icon="el-icon-plus" @click="handleAdd">新增</el-button>
              </span>
            </span>
            <span>
              <el-button-group>
                <el-button icon="el-icon-search"></el-button>
                <el-button icon="el-icon-refresh"></el-button>
              </el-button-group>
            </span>
          </div>
        </div>
      </div>
      <el-card style="margin-top: 10px">
        <el-table
          ref="table"
          :data="tableData"
          style="width: 100%"
          row-key="id"
          lazy
          >>
          <el-table-column type="index" label="#" width="55"> </el-table-column>
          <el-table-column prop="userName" label="用户名"> </el-table-column>
          <el-table-column prop="roleName" label="角色" align="center"></el-table-column>
          <el-table-column prop="gender" label="性别" align="center" width="80">
            <template slot-scope="scope">
              <el-tag type="success" v-if="scope.row.gender == 1">男</el-tag>
              <el-tag type="danger" v-if="scope.row.gender == 0">女</el-tag>
            </template>
          </el-table-column>
          <el-table-column prop="phone" label="电话" align="center"></el-table-column>
          <el-table-column prop="state" label="状态" align="center">
            <template slot-scope="scope">
              <el-switch
                @change="changeSwitch(scope.row)"
                v-model="scope.row.state"
                :active-value="1"
                active-text="正常"
                :inactive-value="0"
                inactive-text="未启用"
              >
              </el-switch>
            </template>
          </el-table-column>
          <el-table-column prop="createTime" label="创建日期" align="center">
            <template slot-scope="scope">
              {{ scope.row.createTime | formatDate }}
            </template>
          </el-table-column>
          <el-table-column label="操作" width="150" align="center">
            <template slot-scope="scope">
              <el-button type="primary" @click="handleEdit(scope.row.id)">编辑</el-button>
              <el-button type="danger" @click="handleDelete(scope.row)">删除</el-button>
            </template>
          </el-table-column>
        </el-table>
      </el-card>
    </el-card>
    <!-- 分页 -->
    <pagination :total="total" :page.sync="listQuery.pageNum" :limit.sync="listQuery.pageSize" @pagination="getList"/>
  </div>
</template>

<script>
import { getUserPageList, createUser, updateUserStatus, deleteUser } from "@/api/user";
import Pagination from "@/components/Pagination"; // el分页
export default {
  data() {
    return {
      tableData: [],
      //条件查询搜索
      total: 0,
      searchKey: '',
      listQuery: {
        pageNum: 1,
        pageSize: 10,
        userName: undefined,
      },
      formData: {},
    };
  },
  methods:{
    getList() {
      getUserPageList(this.listQuery).then((res) => {
        this.tableData = res.result.data.list;
        this.total = res.result.data.total;
        this.listQuery.pageNum = res.result.data.pageNum;
        this.listQuery.pageSize = res.result.data.pageSize;

        setTimeout(() => {
          this.listLoading = false;
        }, 1.5 * 1000);
      });
    },
    //监听switch事件
    async changeSwitch(row) {
      console.log(row.state)
      if(row.id == undefined || row.id == "") {
            this.$message({
            type: "info",
            message: "修改失败！",
          });
      }

      if(row.state == undefined) {
            this.$message({
            type: "info",
            message: "修改失败！",
          });
      }
      updateUserStatus(row.id, row.state).then(res => {

        if (res.code == 20000) {
            this.$message({
              type: "success",
              message: "修改成功!",
            });
        } else{
            this.$message({
            type: "info",
            message: "修改失败！",
          });
        }
      })
    },
    // 新增按钮触发
    handleAdd() {
      this.$router.push({ path: '/user/userAdd'});
    },
    //新增提交
    handleSubmit() {

    },
    //编辑
    handleEdit(id) {
      //带参跳转
      this.$router.push({ path: '/user/userUpdate', query: { id: id }})
    },
    //删除
    handleDelete(row) {
      this.$confirm(
        "此操作将永久删除该用户，是否继续?",
        "提示",
        {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning",
        }
      )
        .then(() => {
          deleteUser(row.id).then((res) => {
            if (res.code == 20000) {
              this.$message({
                type: "success",
                message: "删除成功!",
              });
              this.getList();
            }

          });
        })
        .catch(() => {
          this.$message({
            type: "info",
            message: "已取消删除",
          });
        });
    },
    //点击搜索
    searchFilter() {

      this.listQuery.userName = this.searchKey;
      this.getList();
    },
    //按快捷键
    toQuery() {
      this.searchFilter();
    },
  },
  mounted() {
    this.getList();
  },
  components: {
    Pagination,
  },
}
</script>

<style lang="scss" scoped>
::v-deep .el-switch.is-disabled {
  opacity: 1;
}
::v-deep .el-switch.is-checked .el-switch__core {
  &:hover {
    cursor: pointer;
  }
}
// 工具栏
.head {
  &-container {
    text-indent: -10px;
    > div {
      display: flex;
      align-items: center;
      justify-content: space-between;
      > span {
        display: flex;
        align-items: center;
      }
      .filter {
        &-item {
          margin-left: 10px;
        }
      }
    }
  }
}
</style>
